-- RareRender

local ClassRef = app.mvc.createRenderClass(...)

--data = RareVo
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewBagRareNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._onBtnClick = nil
	self:setContentSize(self._ui["Panel"]:getContentSize())

	self._ui["Panel/Button_jump"]:addClickEventListener(function()
		if self._data[2] then
			executeFunc(self._onBtnClick, "RareRender_jumpTo", self._data[2])
		end
	end)

	self._ui["Panel/Button_explain"]:addClickEventListener(function()
		local posTemp = self._grade:convertToWorldSpace(cc.p(0,0))
		posTemp.y = posTemp.y + self._grade:getContentSize().height
		executeFunc(self._onBtnClick, "RareRender_explain", { data=self._data[2], pos=posTemp })
	end)

	self._grade = self._ui["Panel/grade"]
	self._icon = self._ui["Panel/icon"]
	display.uiAddClick( self._icon, function()
		local posTemp = self._grade:convertToWorldSpace(cc.p(0,0))
		posTemp.x = posTemp.x + self._grade:getContentSize().width
		posTemp.y = posTemp.y + self._grade:getContentSize().height
		executeFunc(self._onBtnClick, "RareRender_explain", { data=self._data[2], pos=posTemp })
	end )

	self._num = self._ui["Panel/Panel_num/num"]
	self._name = self._ui["Panel/name"]
	self._Text_1 = self._ui["Panel/Text_1"]
	self._Text_2 = self._ui["Panel/Text_2"]

	self._text_tips = self._ui["Panel/Text_tips"]
	self._text_lock = self._ui["Panel/Text_lock"]
	self._text_tips:setVisible( false )
	self._text_lock:setVisible( false )

	self._dot = self._ui["Panel/dot"]

	self:refresh()
end

-- data = { dataidx-数据源索引,restrictedreward_show-表中项 }
function ClassRef:refresh()
	if self._data and self._data[2] then
		self:setVisible( true )

		local iconPath = GD:getImagePath( Res.coinIconPath, self._data[2].icon ) -- 图标
		self._icon:loadTexture( iconPath )

		-- local bgPath = Res.Quality_Medal_Icon[ tonumber(  ) ] -- 品质
		-- self._grade:loadTexture( bgPath )

		self._ui["Panel/bg"]:setVisible( 0 == tonumber( self._data[1] )%2 ) -- 偶数ID，bg不可见

		self._name:setString( L(self._data[2].name) )

		if self._data[2].point then
			self._ui["Panel/Button_jump"]:setVisible( true )
		else
			self._ui["Panel/Button_jump"]:setVisible( false )
		end

		if self._data[2].numshow1 and self._data[2].numshow2 then -- 2条描述
			self._Text_1:setVisible( true )
			self._Text_2:setVisible( true )
		elseif not self._data[2].numshow1 and not self._data[2].numshow2 then -- 0条描述
			self._Text_1:setVisible( false )
			self._Text_2:setVisible( false )
		else -- 1条描述
			self._Text_1:setVisible( true )
			self._Text_2:setVisible( false )
		end

		if 1 == tonumber( self._data[2].ID ) then -- 每日任务礼盒
			local rewardNumOfDaylyTask = TaskManager:getDailyTaskRewardNum() -- 每日任务可以领取而没领取的数量
			self._Text_1:setString( string.format( L(self._data[2].numshow1), rewardNumOfDaylyTask ) )

			local canUnlock = TaskManager:getDailyTaskAwardBoxCount()
			self._Text_2:setString( string.format( L(self._data[2].numshow2), canUnlock) )

			self:setNum()
			self:setNewFlagVisible( false )
		elseif 2 == tonumber( self._data[2].ID ) then -- 地下城经验卡
			local numOfCanReceived = 0
			local numOfSotre = 0
			local dungeonsRewardInfo = UD:getUserExpCard()
			if dungeonsRewardInfo then
				numOfCanReceived = dungeonsRewardInfo:getAllCanReceivedCount()
				for i = 1, 3 do
					numOfSotre = numOfSotre + dungeonsRewardInfo:getCumulationInfoByLevel( i )
				end
			else
				numOfCanReceived = 0
				numOfSotre = 3
			end
			self._Text_1:setString( string.format( L(self._data[2].numshow1), numOfCanReceived) )
			self._Text_2:setString( string.format( L(self._data[2].numshow2), numOfSotre) )

			self:setNum( numOfCanReceived )
			if numOfCanReceived and numOfCanReceived > 0 then
				self:setNewFlagVisible( true )
			else
				self:setNewFlagVisible( false )
			end
		elseif 3 == tonumber( self._data[2].ID ) then -- 竞技宝箱
			local maxNum = tonumber( GD:getSystemPar( "tourBoxOpenLimited", 10 ) ) -- 每日开启上限
			local str = table.concat( { UD:getLuckBoxRestTimesByType(Enums.BoxType.pvp), "/", maxNum } )
			self._Text_1:setString( string.format( L(self._data[2].numshow1), str ) )

			local maxNumCanHold = tonumber( GD:getSystemPar( "tourBoxAccumulateLimited", 10 ) ) -- 可持有上限
			local haveNum = app:sendMsg("PvpModel", "getPvpBoxNum")
			local str2 = table.concat( { haveNum, "/", maxNumCanHold } )
			self._Text_2:setString( string.format( L(self._data[2].numshow2), str2) )

			self:setNum()
			self:setNewFlagVisible( false )
		elseif 4 == tonumber( self._data[2].ID ) then -- 活动副本宝箱
			local maxNum = tonumber( GD:getSystemPar( "eventBoxDailyOpenLimited", 10 ) ) -- 每日开启上限
			local str = table.concat( { UD:getLuckBoxRestTimesByType(Enums.BoxType.eventBox), "/", maxNum } )
			self._Text_1:setString( string.format( L(self._data[2].numshow1), str ) )

			local curActivityDungeonsPoint = UD:getActivityDungeonsPoint() -- 活动点数
			local oneBoxNeedPoint = GD:getSystemPar( "eventBoxPointRequired", 200 ) -- 一个包厢需要的点数
			local curAccumulateRewardNum = math.floor( curActivityDungeonsPoint/oneBoxNeedPoint ) -- 累积的个数
			self:setNum( curAccumulateRewardNum )

			local maxAccumulateRewardNum = tonumber( GD:getSystemPar( "eventBoxAccumulateLimited", 20 ) ) -- 累积的上限
			local str2 = table.concat( { curAccumulateRewardNum, "/", maxAccumulateRewardNum } )
			self._Text_2:setString( string.format( L(self._data[2].numshow2), str2) )

			self:setNewFlagVisible( false )
		elseif 101 == tonumber( self._data[2].ID ) then -- 好运钥匙
			local ownNum, usedToday = UD.player:getCharmUsedInfo()
			local canUsedToday = tonumber( GD:getSystemPar( "charmdailymaxcostamount" ) ) -- 今日可使用上限
			local maxNum = tonumber( GD:getSystemPar( "charmmaxamount" ) ) -- 可持有上限

			self._Text_1:setString( string.format( L(self._data[2].numshow1), ( canUsedToday - usedToday ) , canUsedToday ) )
			self._Text_2:setString( string.format( L(self._data[2].numshow2), ownNum ) )

			self:setNum()
			self:setNewFlagVisible( UD:isCharmNumNew() )
		elseif 102 == tonumber( self._data[2].ID ) then -- 地下城宝箱
			local maxNum = tonumber( GD:getSystemPar( "dungeonBoxOpenLimited", 10 ) ) -- 每日开启上限
			local str = table.concat( { UD:getLuckBoxRestTimesByType(Enums.BoxType.dungeon), "/", maxNum } )
			self._Text_1:setString( string.format( L(self._data[2].numshow1), str ) )

			self:setNum()
			self:setNewFlagVisible( false )
		else
			-- print( " ------------------------>>>>> 未知宝物类型 " )
		end
	else
		self:setVisible( false )
	end
end

function ClassRef:setNum( num )
	if num and num > 0 then
		self._ui["Panel/Panel_num"]:setVisible( true )
		self._num:setString( num )
	else
		self._ui["Panel/Panel_num"]:setVisible( false )
	end
end

function ClassRef:setNewFlagVisible( b )
	self._dot:setVisible( b )
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

return ClassRef
